package com.example.amoxicilin.null_test.main;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Resource;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.util.List;

public class ComSearchActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private List<String> comlist;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            String response=(String) msg.obj;
            comlist= JsonTools.jsonSplit(response);
            recyclerView.setAdapter(new RecyclerView.Adapter() {

                class MyViewHolder extends RecyclerView.ViewHolder{

                    public final ImageView clas;
                    public final TextView title;
                    public final TextView info;

                    public MyViewHolder(View itemView) {
                        super(itemView);
                        clas=(ImageView)itemView.findViewById(R.id.resource_label);
                        title=(TextView)itemView.findViewById(R.id.com_title);
                        info=(TextView)itemView.findViewById(R.id.com_info);

                    }
                }


                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_com, parent, false);
                    return new MyViewHolder(view);
                }

                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                    MyViewHolder myViewHolder=(MyViewHolder) holder;
                    final Resource study = JsonTools.getResource(null,comlist.get(position));
                    myViewHolder.title.setText(study.getTitle());
                    myViewHolder.info.setText(study.getIntroduction());
                    myViewHolder.info.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                    switch (study.getLabel1()){
                        case 1:
                            myViewHolder.clas.setImageResource(R.drawable.hua);break;
                        case 2:
                            myViewHolder.clas.setImageResource(R.drawable.ji);break;

                        default:break;
                    }

                    holder.itemView.setOnClickListener(new View.OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            int position=holder.getLayoutPosition();
                            //Toast.makeText(ComSearchActivity.this, "click"+position, Toast.LENGTH_SHORT).show();
                            Intent intent =new Intent();
                            intent.setClass(ComSearchActivity.this,StudyActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("Position",study.getIntroduction());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }

                @Override
                public int getItemCount() {
                    return comlist.size();
                }
            });
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.activity_com_search);
        recyclerView=(RecyclerView)findViewById(R.id.com_search_list);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        Bundle bundle=getIntent().getExtras();
        String search=bundle.getString("Search");
        initData(search);

    }


    private void initData(String key){
        String path = "http://192.168.12.221:8080/tmoocserver/ContentServlet?classid="+key;
        String jsonString = HttpUtils.getJsonContent(path);
        Message message= new Message();
        message.obj=jsonString;
        handler.sendMessage(message);
    }
}
